<?php

session_start();
//Load all php file
include("ClassLoader\loader.php");
$loader = new Loader();
$loader->load();

//Param parser
#remove the directory path we don't want 
$url = $_SERVER['REQUEST_URI'];
$request = str_replace("/buzzPulse/", "", $url);
if (strpos($request, '?') != '') {
    $request = substr($request, 0, strpos($request, "?"));
}
#split the path by '/'
$params = split("/", $request);

$streamId = 1;
if (!$_SESSION["userid"] && strtoupper($params[0]) != "M")
    $params[0] = 'LOGIN';
switch (strtoupper($params[0])) {
    case "USER":
        if (strtoupper($params[1]) == 'LOGIN')
            $params[0] = "USERLOGIN";
        break;
    case "M":
        //$soapRequest = $_REQUEST['request'];
        $server = new bpServer("/buzzPulse/m/");

        $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
        $server->service($HTTP_RAW_POST_DATA);
        return;
        break;
    case "HOME":
        break;
    case "STREAM":
        if ($params[1] != '')
            $streamId = ($params[1]);
        $stream = Stream::withId($streamId);
        $streamView = new StreamView();
        $streamController = new StreamController($stream, $streamView);
        $dateTo = new DateTime();
        $dateFrom = new DateTime();
        $dateFrom->sub(new DateInterval("P10D"));
        $streamController->render($dateFrom->format("d-m-Y H:i:s"), $dateTo->format("d-m-Y H:i:s"), 0, 10);
        if ($params[2])
            $params[1] = $params[2];
        $params[0] = "STREAM";
        break;
    case "SIGNALS":
        $streamId = base64_encode(1);
        $stream = Stream::withId($streamId);
        $streamView = new StreamView();
        $streamController = new StreamController($stream, $streamView);
        $streamController->render("11-09-2013", "13-09-2013", 0, 20);
        $params[0] = "STREAM";
        break;
    case "METRICS":
        if ($params[1] != '')
            $streamId = $params[1];
        $dateTo = new DateTime();
        $dateFrom = new DateTime();
        $dateFrom->sub(new DateInterval("P10D"));
        $filter = array("streamId" => $streamId, "dateFrom" => $dateFrom->format("d-m-Y"), "dateTo" => $dateTo->format("d-m-Y"), "language" => $language,
            "location" => $location, "platform" => $platform, "tag" => $tags, "sentiment" => $sentiment,
            "gender" => $gender, "reach" => $reach);
        $metricsController = new MetricsController(new Metrics(), new MetricsView());
        $metricsController->render($filter);
        if ($params[2])
            $params[1] = $params[2];
        $params[0] = "METRICS";
        break;
    case "ACTIVITY":
        $taskController = new TaskController(new Task(), new TaskView());
        $taskController->render();
        break;
    case "REPORT":

        break;
    case "AJAX":
        if (file_exists("View/AJAX/$params[1]HTML.php"))
            include "View/AJAX/$params[1]HTML.php";
        return;
        break;
    case "":
        $params[0] = "Home";
        break;
    case "LOADSTREAM":
        $dateTo = new DateTime(urldecode($params[3]));
        $dateFrom = new DateTime(urldecode($params[2]));
        $page = 1;
        if ($params[4])
            $page = $params[4];
        $language = 2;
        $location = 0;
        $platform = 0;
        $tags = "";
        $sentiment = 0;
        $gender = 0;
        $reach = 0;
        $index = 10 * ($page - 1);
        $length = 10;
        bpServer::loadStream($params[1], $dateFrom->format("d-m-Y H:i:s"), $dateTo->format("d-m-Y H:i:s"), $language, $location, $platform, $tags, $sentiment, $gender, $reach, $index, $length);
        return;
    case "NEWSTREAM":
        $dateTo = new DateTime();
        $dateFrom = new DateTime(urldecode($params[2]));
        $language = 2;
        $location = 0;
        $platform = 0;
        $tags = "";
        $sentiment = 0;
        $gender = 0;
        $reach = 0;
        $index = 0;
        $length = 10;
        try {
        bpServer::loadStream($params[1], $dateFrom->format("d-m-Y H:i:s"), $dateTo->format("d-m-Y H:i:s"), $language, $location, $platform, $tags, $sentiment, $gender, $reach, $index, $length);
        }
        catch(Exception $err) {
            
        }
        return;
    case "LOADMETRICS":
        $dateTo = new DateTime(urldecode($params[3]));
        $dateFrom = new DateTime(urldecode($params[2]));
        bpServer::loadMetrics($params[1], $dateFrom->format("d-m-Y H:i:s"), $dateTo->format("d-m-Y H:i:s"));
        return;
        break;
    case "LOADTOPSOCIAL":
        $dateTo = new DateTime(urldecode($params[3]));
        $dateFrom = new DateTime(urldecode($params[2]));
        bpServer::loadTopSocial($params[1], $dateFrom->format("d-m-Y"), $dateTo->format("d-m-Y"));
        return;
        break;
    case "LOGIN":
        $userView = UserView::getInstance();
        $userView->display("LoginViewHTML.php");
        break;
    case "SETTINGS":
        $params[0] = "Settings";
        break;
    case "TEST":
        $messageId = '11497146';
        $tagId = '1';
        $streamId = '1  ';
        $dateFrom = new DateTime();
        $dateFrom->sub(new DateInterval("P10D"));
        $dateTo = new DateTime();
        $_filter["streamId"] = $streamId;
        $_filter["dateFrom"] = $dateFrom->format("d-m-Y");
        $_filter["dateTo"] = $dateTo->format('d-m-Y');
        var_dump(Metrics::getSharingMetricsData($_filter));
        return;
        break;
    case "FOLDER":
        $folderController = new FolderController(new Folder(), new FolderView());
        $folderController->render();
        include "/View/Folder/FolderViewHTML.php";
        return;
    case "AUTHORIZETWITTER":
        if (!isset($_GET["oauth_token"])) {
            Twitter::getAuthorization('localhost/buzzPulse/authorizeTwitter');
        }else {
            if(Twitter::getCredentials() == 'true')
                echo "<script>window.close()</script>";
        }
        break;
    case "AUTHORIZEFACEBOOK":
        $login_url = Facebook::getLoginURL();
        header("Location: $login_url");
        if(Facebook::getAuthorization() == 'true')
            echo "<script>window.close()</script>";
        break;
    default:
        $params[0] = "";
        break;
}

$folderController = new FolderController(new Folder(), new FolderView());
$folderController->render();

//Generate View
if (strtoupper($params[0]) !== "LOGIN") {
    $masterView = MasterView::getInstance();
    $masterView->param = $params[0];
    if(strtoupper($params[0]) !== "SETTINGS") {
        $masterView->mainView = "/View/$params[0]/" . $params[0] . "ViewHTML.php";
        $masterView->viewPort = "/View/$params[0]/" . $params[0] . "ViewPortHTML.php";
        if (strtoupper($params[1]) == 'VIEWPORT') {
            $masterView->display("MasterViewPort.php");
        } else if (strtoupper($params[1]) == 'MAINVIEW') {
            $masterView->display("MasterMainView.php");
        } else {
            $masterView->display("MasterPage.php");
        }
    }
    else {
        $masterView->mainView = "/View/$params[0]/" . $params[1] . "ViewHTML.php";
        $masterView->viewPort = "/View/$params[0]/" . $params[0] . "ViewPortHTML.php";
        if (strtoupper($params[2]) == 'VIEWPORT') {
            $masterView->display("MasterViewPort.php");
        } else if (strtoupper($params[2]) == 'MAINVIEW') {
            $masterView->display("MasterMainView.php");
        } else {
            $masterView->display("MasterPage.php");
        }
    }
}
//$masterView->display("LoginPage.php");
//$message = Message::withId(1);
#region Procedures
function getProd($data) {
    return bpServer::getProd($data);
}

function getTrendlineData($_filter) {
    return bpServer::getTrendlineData($_filter);
}

function getTopSocialPlatformsData($_filter) {
    return bpServer::getTopSocialPlatformsData($_filter);
}

function getSharingMetricsData($_filter) {
    return bpServer::getSharingMetricsData($_filter);
}

function getSentimentData($_filter) {
    return bpServer::getSentimentData($_filter);
}

function getGenderDistributionData($_filter) {
    return bpServer::getGenderDistributionData($_filter);
}

function getTopLanguagesData($_filter) {
    return bpServer::getTopLanguagesData($_filter);
}

function getGeographicDistributionData($_filter) {
    return bpServer::getGeographicDistributionData($_filter);
}

function getExposureData($_filter) {
    return bpServer::getExposureData($_filter);
}

function getConversationMapData($_filter) {
    return bpServer::getConversationMapData($_filter);
}

function saveStream($stream) {
    return (array) bpServer::saveStream($stream);
}

function addTask($taskdata) {
    return (array) bpServer::addTask($taskdata);
}

function markComplete($id) {
    return bpServer::markComplete($id);
}

function undoComplete($id) {
    return bpServer::undoComplete($id);
}

function deleteTask($id) {
    return bpServer::deleteTask($id);
}

function getLogin($login) {
    return bpServer::getLogin($login);
}

function getUser() {
    return bpServer::getUser();
}

function addUser($user) {
    return bpServer::addUser($user);
}

function getFolder() {
    return bpServer::getFolder();
}

function addFolder($addfolder) {
    return bpServer::addFolder($addfolder);
}

function listUser() {
    return bpServer::listUser();
}

function listStream($folderid) {
    return bpServer::listStream($folderid);
}

function getFolderId($name) {
    return bpServer::getFolderId($name);
}

function getFolderType() {
    return bpServer::getFolderType();
}
    
function addTag($messageId, $tagId, $streamId) {
    return bpServer::addTag($messageId, $tagId, $streamId);
}

function deleteUser($userid) {
    return bpServer::deleteUser($userid);
}

function getTag() {
    return bpServer::getTag();
}

function createTag($tagname) {
    return bpServer::createTag($tagname);
}

function deleteTag($tagid) {
    return bpServer::deleteTag($tagid);
}

function deleteStream($streamid) {
    return bpServer::deleteStream($streamid);
}

function getTimeZoneName($timezone) {
    return bpServer::getTimeZoneName($timezone);
}            
                
function getTimeZone($name, $location) {
    return bpServer::getTimeZone($name, $location);
}
    
function getInfo() {
    return bpServer::getInfo();
}

function copyStream($streamid, $folderid) {
    return bpServer::copyStream($streamid, $folderid);
}   

function postTweet($message) {
    return bpServer::postTweet($message);
} 

function getTwitters() {
    return bpServer::getTwitters();
}

function deleteTwitter($twitterid) {
    return bpServer::deleteTwitter($twitterid);
}

function deleteFacebook($facebookid) {
    return bpServer::deleteFacebook($facebookid);
}
    
function getFacebooks() {
    return bpServer::getFacebooks();
}
    
function postFacebookStatus($message, $link) {
    return bpServer::postFacebookStatus($message, $link);
} 

function getAccounts() {
    return bpServer::getAccounts();
}
?>